package cn.tianjindong.blog_portal.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.pojo.BlogArticle;
import cn.tianjindong.blog.pojo.BlogTag;
import cn.tianjindong.blog_portal.service.ArticleService;
import cn.tianjindong.blog_portal.service.TagsService;

@Controller
public class TagController {
	
	@Autowired
	private ArticleService articleService;
	
	@Autowired
	private TagsService tagsService;
	/**
	 * 前往“标签云”页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/tags.html")
	public String toTagsPage(Model model){
		List<BlogTag> tagList = tagsService.getTagList();
		model.addAttribute("list", tagList);
		return "/tags";
	}
	
	/**
	 * 前往标签对应的文章列表
	 * @return
	 */
	@RequestMapping("/tag-article/{tagId}.html")
	public String toTagArticleList(Model model,@PathVariable Long tagId,@RequestParam(defaultValue = "1") Integer currentPage,
			@RequestParam(defaultValue = "8") Integer countItem){
		PageView<BlogArticle> pageView = articleService.getArticlePageViewByTagId(tagId,currentPage,countItem);
		model.addAttribute("pageView", pageView);
		BlogTag tag = tagsService.getTagById(tagId);
		if(tag == null){
			return "/404";
		}else{
			model.addAttribute("tag", tag);
			return "/tag-article-list";
		}
	}
}
